package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.constants.ScanRecordTypeEnum;
import cn.com.chengmandian.bike.event.ScanErrorEvent;
import cn.com.chengmandian.bike.model.*;
import cn.com.chengmandian.bike.mapper.EbScanRecordMapper;
import cn.com.chengmandian.bike.service.*;
import cn.com.chengmandian.core.redis.cache.AgentParamCacheService;
import cn.com.chengmandian.core.web.pojo.AgentUser;
import cn.com.chengmandian.core.web.pojo.BikeAreaParam;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 电单车运营-用户扫码记录表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-05-17
 */
@Service
public class EbScanRecordServiceImpl extends ServiceImpl<EbScanRecordMapper, EbScanRecord> implements IEbScanRecordService {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private IEbOperationAreaService operationAreaService;
    @Autowired
    private IEbMemberInfoService memberInfoService;
    @Autowired
    private AgentParamCacheService agentParamCacheService;

    @Override
    public EbScanRecord generateRecord(ScanRecordTypeEnum recordTypeEnum, Integer orderId, AgentUser agentUser, EbDeviceBike ebDeviceBike, BigDecimal lng, BigDecimal lat) {
        return this.generateRecord(recordTypeEnum.getErrCode(), orderId, agentUser, ebDeviceBike, lng, lat);
    }

    @Override
    public EbScanRecord generateRecord(Integer errCode, Integer orderId, AgentUser agentUser, EbDeviceBike ebDeviceBike, BigDecimal lng,BigDecimal lat){
        ScanRecordTypeEnum scanRecordTypeEnum = ScanRecordTypeEnum.getByErrCode(errCode);
        if(scanRecordTypeEnum == null) return null;
//        // 未投放508异常展示不写入 未投放已被细分
//        if(scanRecordTypeEnum.getErrCode().equals(Integer.valueOf(508))) return null;
        String message = "";
        if(scanRecordTypeEnum.getErrCode().equals(ScanRecordTypeEnum.NO_NEARBY.getErrCode())){
            BikeAreaParam bikeAreaParam = agentParamCacheService.getParam(ebDeviceBike.getAgentId(), ebDeviceBike.getAreaId());
            message = scanRecordTypeEnum.getName().replace("%s", bikeAreaParam.getUserOpenLockDistance().toString());
        }else {
            message = scanRecordTypeEnum.getName();
        }
        EbScanRecord ebScanRecord = new EbScanRecord();
        ebScanRecord.setAgentId(LocalMapUtil.getAgentInfo().getId());
        ebScanRecord.setGirdId(ebDeviceBike.getGirdId());
        ebScanRecord.setStationId(ebDeviceBike.getStationId());
        ebScanRecord.setAreaId(ebDeviceBike.getAreaId());
        ebScanRecord.setMemberId(agentUser.getId());
        ebScanRecord.setLng(lng);
        ebScanRecord.setLat(lat);
        ebScanRecord.setBikeId(ebDeviceBike.getId());
        ebScanRecord.setBikeNo(ebDeviceBike.getBikeNo());
        ebScanRecord.setOrderId(orderId);
        ebScanRecord.setCreateType(scanRecordTypeEnum.getCode());
        ebScanRecord.setMessage(message);
        this.save(ebScanRecord);
        if(ScanRecordTypeEnum.NO_ONLINE.getErrCode().equals(errCode)){
            EbOperationArea ebOperationArea = operationAreaService.getById(ebDeviceBike.getAreaId());
            EbMemberInfo ebMemberInfo = memberInfoService.getById(agentUser.getId());
            applicationEventPublisher.publishEvent(new ScanErrorEvent(this,ebOperationArea.getDdOpsToken(),ebOperationArea.getName(),ebDeviceBike.getBikeNo(),ebMemberInfo.getName(),
                    ebMemberInfo.getMobile(),lng,lat,DateUtil.date()));
        }
        return ebScanRecord;
    }
}
